<?php


    /**
    * Main class for working with site pages named "posts".
    * @author NU SIT Bar
    * @version 1.0
    * @created 17.Jul.2012 17:39:44 h
    */
    class Pages_model extends CI_Model
    {

        function __construct()
        {
            parent::__construct();
        }



        /**
        * 
        * @param post_type
        */
        function view_all($post_type)
        {
            $this->db->where('post_type',$post_type);
            $this->db->where('parent_id',0);
            $res = $this->db->get('posts')->result_array();

            return $res;
        }

        /**
        * 
        * @param post_type
        */
        function view_all_children($parent_ID)
        {
            $this->db->where('parent_id',$parent_ID);
            $res = $this->db->get('posts')->result_array();

            return $res;
        }

        /**
        * 
        * @param post_ID
        */
        function view_edit($post_ID)
        {           
            $this->db->where('ID',$post_ID);
            $res = $this->db->get('posts')->row_array();
            return $res;
        }

        /**
        * 
        * @param post_title
        * @param name
        * @param content
        * @param parent_ID
        * @param post_type
        */
        function create_page($post_title, $name, $content, $parent_ID, $post_type)
        {

            $this->db->insert('posts', array(
                    'post_title' => $post_title,
                    'name' => $name,
                    'content' => $content,
                    'parent_ID' => $parent_ID,
                    'date_time' => time(),
                    'post_type' => $post_type,
                    'post_status' => 'publish'
                ));

            return TRUE;

        }

        /**
        * 
        * @param ID
        * @param post_title
        * @param name
        * @param content
        * @param parent_ID
        */
        function update_page($ID, $post_title, $name, $content, $parent_ID, $post_type)
        {
            $this->db->where('ID',$ID);
            $this->db->update('posts', array(
                    'post_title' => $post_title,
                    'name' => $name,
                    'content' => $content,
                    'parent_ID' => $parent_ID,
                    'modified' => time(),
                    'post_type' => $post_type,
                    'post_status' => 'publish'
                ));

            return TRUE;
        }

        /**
        * 
        * @param ID
        */
        function delete_page($ID)
        {

            //select all children if there is any
            $childrens = $this->db->get_where('posts',array('parent_ID'=>$ID))->result_array();

            //if children exists, unlin gallery, folders then delete children
            if(count($childrens)>0):
                foreach($childrens as $children):

                    // First Unlink gallery
                    $res = $this->db->get_where('gallery',array('posts_ID'=>$children['ID']))->row_array();

                    if(isset($res['ID'])){
                        $this->db->where('ID',$res['ID']);
                        $this->db->update('gallery',array('posts_ID'=>NULL)); 
                    }

                    // Unlink documents second
                    $res = $this->db->get_where('folders',array('posts_ID'=>$children['ID']))->row_array();
                    if(isset($res['ID'])){
                        $this->db->where('ID',$res['ID']);
                        $this->db->update('folders',array('posts_ID'=>NULL)); 
                    }

                    $this->db->where('ID',$children['ID']);
                    $this->db->delete('posts'); 

                    endforeach;
                endif;

            // First Unlink gallery
            $res = $this->db->get_where('gallery',array('posts_ID'=>$ID))->row_array();

            if(isset($res['ID'])){
                $this->db->where('ID',$res['ID']);
                $this->db->update('gallery',array('posts_ID'=>NULL)); 
            }

            // Unlink documents second
            $res = $this->db->get_where('folders',array('posts_ID'=>$ID))->row_array();
            if(isset($res['ID'])){
                $this->db->where('ID',$res['ID']);
                $this->db->update('folders',array('posts_ID'=>NULL)); 
            }

            $this->db->where('ID',$ID);
            $this->db->delete('posts');

            return TRUE;

        }

    }
?>